//
//  ZXStatusBar.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXStatusBar.h"

static ZXStatusBar * statusBar;//静态属性

@implementation ZXStatusBar

-(instancetype)init
{
    if (self = [super initWithFrame:[UIApplication sharedApplication].statusBarFrame])
    {
//        设置窗口的level 大于UIWindowLevelStatusBar 这个窗口就会在系统状态栏上面（大于系统的优先级）
        self.windowLevel = UIWindowLevelStatusBar + 10;
    }
    
    return self;
}

+(void)showTip:(NSString *)tip
{
    statusBar = [[ZXStatusBar alloc]init];
    
    UIView * blackView = [[UIView alloc]init];
    blackView.frame = statusBar.bounds;
    blackView.backgroundColor = [UIColor blackColor];
//    超出父控件的范围裁剪掉
    blackView.clipsToBounds = YES;
    
    UILabel * label = [[UILabel alloc]init];
    label.frame = blackView.bounds;
    label.text = tip;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:11];
    [blackView addSubview:label];
    label.transform = CGAffineTransformMakeTranslation(0, 20);
    
    //    设置到窗口上
    [statusBar addSubview:blackView];
    //显示窗口
    [statusBar makeKeyAndVisible];
    
    [UIView animateWithDuration:1 animations:^{
//        复位
        label.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
//        0.5秒以后移除自定义的状态栏
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:1 animations:^{
                blackView.transform = CGAffineTransformMakeTranslation(0, -20);
               
            } completion:^(BOOL finished) {
                statusBar = nil;
            }];
        });
        
        
    }];
}

-(void)dealloc
{
    ZXLog(@"已经销毁了");
}
@end
